/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author jefoutta
 */
import  java.io.*;
import  java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import  org.jdom.*;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class OS implements Runnable{

    private Interpreter interpreter = new Interpreter();
    private Scheduling scheduling = new Scheduling();
    private boolean[] bol;
    public static String temptemp = "";
    private File file;
    private SAXBuilder builder = new SAXBuilder();
    private Document doc;

    public void run(){
        Log.create("jefox.log");
        file = new File("/media/jef_stuff/Documents/NetBeansProjects/jefox/file system/sys.xml");
        try {
            doc = builder.build(file);
        } catch (JDOMException ex) {
            Logger.getLogger(OS.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(OS.class.getName()).log(Level.SEVERE, null, ex);
        }
        //System.out.println(XMLParser.parser(doc).toString());
        Scheduling.defQueues(XMLParser.parser(doc));
        while(true){
            bol = scheduling.check(1);
            if(bol[0]){
                try{
                    Integer temp = null;
                    //interpreter.interpreter(Scheduling.proc_queue.peek().getProcessName(), Scheduling.proc_queue.peek().getSyn(), Scheduling.proc_queue.dequeue().getFileName());
                    if(!ProcQueue.procQueues.get(Scheduling.actQueue).isEmpty()){
                    //if(!Scheduling.proc_queue.isEmpty()){
                        temp = ProcQueue.procQueues.get(Scheduling.actQueue).poll();
                        //temp = Scheduling.proc_queue.poll();
                        temptemp = temptemp + temp;
                        Scheduling.procAct = temp;
                    }
                    if(bol[1]){
                        interpreter.toExec();
                    }
                        //interpreter.toExec();
                    else{
                        interpreter.interpreter(Scheduling.procAct);
                    }
                        
                    
                }catch(IOException e){
                    e.printStackTrace();
                }
            }else
                    if((bol[2])&&!(bol[0])){
                        try {
                            if(bol[3]){
                                Scheduling.tickWaiting();
                            }
                            else{
                                if(Scheduling.procAct !=null){
                                    PCB.proc_id.get(Scheduling.procAct).leaveTick(Scheduling.totalTicks);
                                    PCB.proc_id.get(Scheduling.procAct).takeTick(Scheduling.totalTicks);
                                    if((!Scheduling.quantum)){
                                        interpreter.toExec();
                                        //System.out.println("aqui nop");
                                    }
                                }
                            }
                                
                        } catch (IOException ex) {
                            Logger.getLogger(OS.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
        }
    }
}